import json
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.contrib import auth
# Create your views here.
from stu_info_ctr.models import User


# 注册用户
def regist_index_view(request):
    req_type = request.method

    username = request.POST.get('uname')
    pwd = request.POST.get('pwd')
    ph = request.POST.get('phone')
    ad = request.POST.get('address')
    if req_type == 'GET':
        return render(request, 'regist.html')
    elif req_type == 'POST':
        if username and pwd:
            user = User(uname=username, password=pwd, phone=ph, adress=ad)
            user.save()
            return render(request, 'page_jump.html')
        else:
            return HttpResponse('注册失败!!!')


# 登录并显示欢迎页面
def login_index_view(request):
    ret_type = request.method
    user_list = User.objects.values()
    if ret_type == 'GET':
        return render(request, 'login.html')
    elif ret_type == 'POST':
        username = request.POST.get('uname', None)
        pwd = request.POST.get('pwd', None)
        if username and pwd:
            qs = user_list.filter(uname=username, password=pwd)
            if qs:
                request.session['username'] = username
                return render(request, 'welcome.html', locals())
            else:
                return HttpResponse('用户不存在！！')
        else:
            return HttpResponse('用户名密码错误！！')
    else:
        return HttpResponse('请求类型错误！！')
